package com.wuhunyu.service.impl;

import com.wuhunyu.service.WeChatService;
import com.wuhunyu.servicebase.exception.Assert;
import com.wuhunyu.utils.WeChatConstant;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * 微信登录服务类实现类
 *
 * @author wuhunyu
 * @version 1.0
 * @date 2021-01-23 23:27
 */
@Service("weChatService")
public class WeChatServiceImpl implements WeChatService {

    @Override
    public String genraterCode() {
        // 微信开放平台授权baseUrl  %s相当于?代表占位符
        String baseUrl = "https://open.weixin.qq.com/connect/qrconnect" +
                "?appid=%s" +
                "&redirect_uri=%s" +
                "&response_type=code" +
                "&scope=snsapi_login" +
                "&state=%s" +
                "#wechat_redirect";
        // 重定向的地址需要进行URLEncoder编码
        String encodeUrl = null;
        try {
            encodeUrl = URLEncoder.encode(WeChatConstant.REDIRECT_URL, "utf-8");
        } catch (UnsupportedEncodingException e) {
            Assert.isRight(true, "重定向地址编码错误:" + e.getMessage());
        }
        String url = String.format(
                baseUrl,
                WeChatConstant.APP_ID,
                encodeUrl,
                "wuhunyu"
        );
        return url;
    }
}
